<?php

namespace XWAM\Controller;

/**
 * 订单控制器
 * 
 * @since 1.0.0 <2015-4-2> SoChishun Added.
 */
class OrderController extends AppbaseController {

    /**
     * 获取order_list查询表单数据
     * @return array 包含search和where数组
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    function order_list_search() {
        $search = I('get.');
        if ($search['keyword']) {
            $where['name'] = array('like', '%' . $search['keyword'] . '%');
        }
        $tab = I('tab');
        if ($tab) {
            $search['tab'] = $tab;
        }
        if ($search['tel']) {
            $where['telphone'] = $search['tel'];
        }
        if ($search['name']) {
            $where['personalName'] = $search['name'];
        }
        if ($search['sn']) {
            $where['serialNo'] = $search['sn'];
        }
        if ($search['orderAmountMin']) {
            $where['orderAmount'][] = array('EGT', $search['orderAmountMin']);
        }
        if ($search['orderAmountMax']) {
            $where['orderAmount'][] = array('ELT', $search['orderAmountMax']);
        }
        if ($search['sellerUserName']) {
            $where['sellerUserName'] = $search['sellerUserName'];
        }
        if ($this->RoleInfo['SEAT']) {
            $where['sellerID'] = $this->user_login_data['id'];
        } else if ($this->RoleInfo['ADMIN']) {
            $where['siteID'] = $this->user_login_data['siteID'];
        }
        $tpl = '';
        switch ($tab) {
            case 'unconfirmed': // 待审核(状态:0=未审核)
                $tpl = 'order_list_unconfirmed_partial';
                $where['state'] = 0;
                break;
            case 'returnconfirm': // 审核退回(状态:24=核单失败,34=仓库退回)
                $tpl = 'order_list_returnconfirm_partial';
                $where['state'] = array('IN', '24,34');
                break;
            case 'unfilled': // 待发货(状态:20=核单通过)
                $tpl = 'order_list_unfilled_partial';
                $where['state'] = 20;
                break;
            case 'delivered': // 已发货(状态:30=已发货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_delivered_partial';
                $where['state'] = 30;
                break;
            case 'signed': // 客户签收(状态:50=已签收)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_partial';
                $where['state'] = 50;
                break;
            case 'refused': // 拒绝签收(状态:54=客户拒签)
                $table = 'vorder_logistics';
                $tpl = 'order_list_signed_partial';
                $where['state'] = 54;
                break;
            case 'returned': // 已退货(状态:55=客户退货)
                $table = 'vorder_logistics';
                $tpl = 'order_list_returned_partial';
                $where['state'] = 55;
                break;
            case 'canceled': // 已取消(40=座席取消,41=买家取消,42=逾期取消,43=订单无效取消)
                $tpl = 'order_list_canceled_partial';
                $where['state'] = array('BETWEEN', '40,49');
                break;
            case 'settled': // 已结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_partial';
                $where['orderSettlementState'] = array('GT', 0);
                break;
            case 'unsettled': // 未结算(0=未收款,1=已收款,2=部分收款)
                $tpl = 'order_list_settled_partial';
                $where['orderSettlementState'] = 0;
                break;
            case 'dispatch': // 调度订单
                $tpl = 'order_list_dispatch_partial';
                $where['oldSellerID'] = array('GT', 0);
                break;
        }
        return array('search' => $search, 'where' => $where, 'tpl' => $tpl);
    }

    /**
     * order_list操作
     * @since 1.0.0 <2015-4-2> SoChishun Added.
     */
    public function order_list() {
        $asearch = $this->order_list_search(); // 获取查询表单数据
        $m_order = new \XWAM\Model\OrderModel();
        $list = $m_order->get_paging_list($page, array('where' => $asearch['where']), array('page_params' => $asearch['search']));
        $this->assign('list', $list);
        $this->assign('pager', $page->show());
        $this->display_cpp();
    }

    /**
     * order_edit操作
     * @param int $id 主键编号
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_add($user='') {
        $data=array('user_name'=>$user);
        $this->assign('data',$data);
        $this->display();
    }
    
    function order_add_save(){
        $m_order=new \XWAM\Model\OrderModel();
        $result=$m_order->add_order();
        $this->dialogJump($result['status']);
    }

    /**
     * order_edit操作
     * @param int $id 主键编号
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_view($id=0) {
        $m_order=new \XWAM\Model\OrderModel();
        $data=$m_order->find($id);
        $list=$m_order->table('t_porg_order_product_list')->where(array('order_id'=>$id))->select();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->display();
    }

    /**
     * order_edit_save操作
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_edit_save() {
        $result = D('Order')->save_item();
        $this->base_redirect_return($result, U('Order/order_list'));
    }

    /**
     * order_delete操作
     * @param int $id 主键编号
     * @since 1.0 <2015-4-2> SoChishun Added.
     */
    public function order_delete($id = 0) {
        $this->base_common_delete($id, 'Order');
    }

    function change_status($id, $status) {
        $m_order = new \XWAM\Model\OrderModel();
        $this->ajaxReturn($m_order->where()->setField('status', $status));
    }

}
